# -*- coding: utf-8 -*-  
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'libs'))

import web #main web framework 
import handlers
from admin.main import app_admin #adsmin application
from google.appengine.api import memcache


#urls patch
urls = (
        '/(.*)/', handlers.Redirect,
        '/admin',app_admin,
        
        '/themes/(.*\.(css|js|jpg|gif|png))', handlers.ReadThemeFile, #静态文件
        '/component/(.*\.(css|js|jpg|gif|png|htm|html))',handlers.ReadComponentFile,
        
        '/feed',handlers.ViewFeedPage,#feed 
        
        '/', handlers.ViewIndexPage,#首页
    
        '/category/([^/]*)',handlers.ViewCategoryArchivePage,#分类
        '/archive/(\d+)/(\d+)',handlers.ViewDateArchivePage,#时间归档
        '/tag/([^/]*)',handlers.ViewTagArchivePage,#标签

        '/(.*\.html)',handlers.ViewArticlePage,#文章
        '/(\w+)',handlers.ViewArticlePage,#shorturl文章
        '/(.*\.html)/comment',handlers.AddArticleComment,#文章
       
        
        
        '/(.*)',handlers.ViewNofoundPage#404page
)
#open debug mode
web.config.debug = True
app = web.application(urls, locals())

#Using session with debug mode
if web.config.get('_session') is None:
    store = web.session.MemcacheStore(memcache)
    session = web.session.Session(app, store, initializer={'count': 0})
else:
    session = web.config._session

#Using session with sub application
def session_hook():
    web.ctx.session = session
app.add_processor(web.loadhook(session_hook))


if __name__ == "__main__": app.cgirun()